/*
 * File: PurchaseTest.java Author: Robert Bittle <guywithnose@gmail.com>
 */
package faceBook.tests;

import static org.junit.Assert.*;

import java.sql.Timestamp;
import org.junit.Test;
import tests.AppEngineTest;
import faceBook.Purchase;
import faceBook.User;

/**
 * The Class PurchaseTest.
 */
@SuppressWarnings("static-method")
public class PurchaseTest extends AppEngineTest
{

  /**
   * Tests the getID method.
   */
  @Test
  public void testGetID()
  {
    Purchase purchase = new Purchase(4);
    assertEquals(4, purchase.getID());
  }

  /**
   * Tests the getMessageCount method.
   */
  @Test
  public void testGetMessageCount()
  {
    Purchase purchase = new Purchase();
    purchase.setMessageCount(54);
    assertEquals(54, purchase.getMessageCount());
  }

  /**
   * Tests the getOrderId method.
   */
  @Test
  public void testGetOrderId()
  {
    Purchase purchase = new Purchase();
    purchase.setOrderId(1231231454561L);
    assertEquals(1231231454561L, purchase.getOrderId());
  }

  /**
   * Tests the getTime method.
   */
  @Test
  public void testGetTime()
  {
    Purchase purchase = new Purchase();
    purchase.setTime(new Timestamp(450000000000L));
    assertEquals(new Timestamp(450000000000L).toString(), purchase.getTime()
        .toString());
  }

  /**
   * Tests the getUser method.
   */
  @Test
  public void testGetUser()
  {
    Purchase purchase = new Purchase();
    purchase.setUser(new User(123));
    assertEquals(123, purchase.getUser().getID());
  }

  /**
   * Tests the getPrice method.
   */
  @Test
  public void testGetPrice()
  {
    Purchase purchase = new Purchase();
    purchase.setPrice(10.57);
    assertEquals(10.57, purchase.getPrice(), .0000001);
  }

  /**
   * Tests the dataBase method.
   */
  @Test
  public void testDataBase()
  {
    User user1 = new User(123);
    User user2 = new User(789);
    user1.save();
    user2.save();
    Purchase purchase = new Purchase();
    purchase.setMessageCount(54);
    purchase.setPrice(10.57);
    purchase.setOrderId(1231231454561L);
    purchase.setTime(new Timestamp(450000000000L));
    purchase.setUser(user1);
    assertTrue(purchase.save());// Save the object
    Purchase purchase2 = new Purchase(purchase.getID());// Load the object
    assertEquals(54, purchase2.getMessageCount());
    assertEquals(10.57, purchase2.getPrice(), .001);
    assertEquals(1231231454561L, purchase2.getOrderId());
    assertEquals(new Timestamp(450000000000L), purchase2.getTime());
    assertEquals(123, purchase2.getUser().getID());
    purchase2.setMessageCount(540);
    purchase2.setPrice(15.57);
    purchase2.setOrderId(541231231454561L);
    purchase2.setTime(new Timestamp(480000000000L));
    purchase2.setUser(user2);
    assertTrue(purchase2.save());// Save the object
    Purchase purchase3 = new Purchase(purchase.getID());// Load the object again
    assertEquals(540, purchase3.getMessageCount());
    assertEquals(15.57, purchase3.getPrice(), .001);
    assertEquals(541231231454561L, purchase3.getOrderId());
    assertEquals(new Timestamp(480000000000L), purchase3.getTime());
    assertEquals(789, purchase3.getUser().getID());
    purchase.delete();// Delete the object
    Purchase purchase4 = new Purchase(purchase.getID());
    assertEquals(0, purchase4.getMessageCount());
    assertEquals(0, purchase4.getPrice(), .001);
    assertEquals(0, purchase4.getOrderId());
    assertEquals(null, purchase4.getTime());
    assertEquals(null, purchase4.getUser());
    user1.delete();
    user2.delete();
  }

  /**
   * Tests the toJSONString method.
   */
  @Test
  public void testToJSONString()
  {
    Purchase purchase = new Purchase(123);
    purchase.setMessageCount(54);
    purchase.setPrice(10.57);
    purchase.setOrderId(1231231454561L);
    purchase.setTime(new Timestamp(454587454514L));
    purchase.setUser(new User(123));
    assertEquals(
        "{\"time\":\"1984-05-28 10:17:34.514\",\"price\":10.57,\"ID\":123,\"messageCount\":54,\"user\":{\"id\":123,\"useDefaultDB\":false,\"useReply\":false},\"orderId\":1231231454561}",
        purchase.toJSONString());
  }

  /**
   * Tests the getAllPurchasesForUser method.
   */
  @Test
  public void testGetAllPurchasesForUser()
  {
    User user = new User(122);
    user.save();
    Purchase purchase = new Purchase();
    purchase.setUser(user);
    purchase.setMessageCount(500);
    purchase.save();
    User user2 = new User(123);
    user2.save();
    Purchase purchase2 = new Purchase();
    purchase2.setUser(user2);
    purchase2.setMessageCount(500);
    purchase2.save();
    User user3 = new User(124);
    user3.save();
    Purchase purchase3 = new Purchase();
    purchase3.setUser(user3);
    purchase3.setMessageCount(500);
    purchase3.save();
    assertEquals(500, Purchase.getMessageCountForUser(new User(123)));
    purchase.delete();
    purchase2.delete();
    purchase3.delete();
    user.delete();
    user2.delete();
    user3.delete();
  }

}
